/**
 * 微信消息相关服务
 * @authors 杨兴洲（of2502）
 * @date    2017/3/2 14:22
 * @version 1.0
 */
import * as rp from "request-promise-native";
const HOST = 'https://api.weixin.qq.com/cgi-bin';
const FILE_HOST = 'https://file.api.weixin.qq.com/cgi-bin';

export default class WeixinMsgService {

    /**
     * 获取公众号自动回复规则
     * @param access_token
     * @returns {Promise<any>}
     */
    static getAutoReplyRules({access_token}): Promise<any> {
        return rp({
            method: 'GET',
            uri: `${HOST}/get_current_autoreply_info`,
            qs: {access_token},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);

    }

    /**
     * 根据tag群发
     * @param access_token
     * @param message
     * @param send_ignore_reprint
     * @param is_to_all
     * @param tag_id
     * @returns {Promise<any>}
     */
    static massSendByTag({access_token, message, send_ignore_reprint = 0, is_to_all = false, tag_id}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/message/mass/sendall`,
            qs: {access_token},
            body: {
                filter: {is_to_all, tag_id},
                send_ignore_reprint,
                ...message
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 根据openid群发
     * @param access_token
     * @param touser
     * @param message
     * @param send_ignore_reprint
     * @returns {Promise<Promise<any>>}
     */
    static async massSendByID({access_token, touser, message, send_ignore_reprint = 0}): Promise<any> {
        if (message.msgtype === 'mpvideo') {
            const data: any = await WeixinMsgService.transformVideo({access_token, ...message.mpvideo});
            message.mpvideo.media_id = data.media_id;
        }
        return rp({
            method: 'POST',
            uri: `${HOST}/message/mass/send`,
            qs: {access_token},
            body: {
                touser,
                send_ignore_reprint,
                ...message
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);

    }

    /**
     * 预览 openId群发
     * @param access_token
     * @param touser
     * @param message
     * @returns {Promise<any>}
     */
    static massSendPreview({access_token, touser, message}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/message/mass/preview`,
            qs: {access_token},
            body: {
                touser,
                ...message
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }


    /**
     * 删除群发消息
     * @param access_token
     * @param msg_id
     * @returns {Promise<any>}
     */
    static massSendDelete({access_token, msg_id}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/message/mass/delete`,
            qs: {access_token},
            body: {
                msg_id
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 查询群发消息发送状态
     * （获取不到已删除状态）
     */
    static massSendStatus({access_token, msg_id}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/message/mass/get`,
            qs: {access_token},
            body: {
                msg_id
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    // 处理视频的media_id
    static transformVideo({access_token, media_id, title, description}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${FILE_HOST}/media/uploadvideo`,
            qs: {access_token},
            body: {
                media_id,
                title,
                description
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

}
